
const {getGoods,addGoods,deleteGoods,updateGoods,searchGoods,searchGoodsSell,getGoodsByPageSell,deleteGoodsSell,getGoodsId,updateGoodsSell} = require("../dao/goodsDao");

/*
 *++++++++++++++++++++++++++++++++++++++++++++++++刘磊++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 */ 
// 渲染页面
module.exports.getGoods = async params => {
    // 判断参数中的 searchValue 是否有值
    if (params.searchValue) {
        // searchValue 有值，就表示当前的请求是在做“搜索学生”
        return await searchGoods(params);
    }
    //  else if (params.pageSize) {
    //     // 当前端传递的参数中有 pageSize 属性，则表示当前是在做分页请求
    //     return await getStudentsByPage(params);
    // }
     else {
        // searchValue 没有值，就表示当前的请求是在做“获取所有学生”
        return await getGoods();
    }
}
// 添加学生
module.exports.addGoods = async function (student){
    const data =  await addGoods(student)
    if(data._id){
        return true
    }
    return false
};

// 删除学生
module.exports.deleteGoods = async function (_id){
    const data =   await deleteGoods(_id)
    if(data.ok==1){
        return true
    }else{
        return false
    }
    
};

// 修改学生
module.exports.updateGoods = async function (student){
    const data =   await updateGoods(student)
    if(data.ok==1){
        return true
    }else{
        return false
    }
    
};
// 搜索（获取部分学生）
module.exports.searchStudents = async ({ searchType, searchValue, currentPage, pageSize }) => {
    // 精确查询
    // return await studentsModel.find({ [searchType]: searchValue });
    // 模糊查询
    // 满足搜索条件的数据总条数
    const total = (await studentsModel.find({ [searchType]: { $regex: searchValue, $options: '$i' } })).length;
    // 搜索到的分页学生数据
    const rows = await studentsModel
        .find({ [searchType]: { $regex: searchValue, $options: '$i' } })
        .populate('classesId')
        .limit(pageSize - 0)   // limit(number) 用于设置请求数据的条数
        .skip((currentPage - 1) * pageSize);   // 用于设置跳过多少条数据
    console.log(rows);
    return { total, rows };
}

/*
 *++++++++++++++++++++++++++++++++++++++++++++++++刘磊++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 */ 
/**
 * ++++++++++++++++++++++++++++++++++李兴乾+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */
//渲染上架商品---lxq
module.exports.getGoodsSell = async function (goods){
    if(goods.searchType){
        return await searchGoodsSell(goods)
    }else if(goods.pageSize){
        return await getGoodsByPageSell(goods)
    }
};
//下架商品
module.exports.deleteGoodsSell = async function (goods){
    const data =   await deleteGoodsSell(goods)
    if(data.ok==1){
        return true
    }else{
        return false
    }
}
//通过id查找商品getGoodsId
module.exports.getGoodsId = async function (goods){
    return await getGoodsId(goods)
}
//修改上架商品
module.exports.updateGoodsSell = async function (goods){
    return await updateGoodsSell(goods)
}
/**
 * ++++++++++++++++++++++++++++++++++李兴乾+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */
